package com.edu.hre.struts.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.cache.PassiveDynaCache;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreSubunit;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.StructureUpdateForm;

public class StructureUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(StructureUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private static final PassiveDynaCache passiveDynaCache = BeanFinder.getBean(PassiveDynaCache.BEAN_NAME, PassiveDynaCache.class);
	private final String ACTION_NAME = "structureUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		StructureUpdateForm beanForm = (StructureUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			int id = Integer.parseInt(request.getParameter("id"));
			int level = Integer.parseInt(request.getParameter("level"));
			beanForm.setId(id);
			beanForm.setLevel(level);
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))){
			beanForm.setMessage("Name of unit can not be blank!");
			return mapping.findForward(ACTION_NAME);
		}
		HreSubunit tmp = baseService.getFirstPojo("name", Compare.EQUAL, CommonUtils.normalizeString(beanForm.getName()), null, null, 0, HreSubunit.class);
		if(tmp != null){
			beanForm.setMessage("The name is exist, please enter another name!");
			return mapping.findForward(ACTION_NAME);
		}
		HreSubunit newObj = new HreSubunit();
		newObj.setParentId(beanForm.getId());
		newObj.setName(CommonUtils.normalizeString(beanForm.getName()));
		newObj.setDescription(CommonUtils.normalizeString(beanForm.getDescription()));
		newObj.setDateCreated(new Date());
		newObj.setDateModified(new Date());
		newObj.setLevel(Short.valueOf(String.valueOf(beanForm.getLevel() + 1)));

		boolean status = baseService.saveOrUpdate(newObj);

		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}
		passiveDynaCache.removeCachedItem(RequestUtils.Attribute.LIST_SUB_UNITS_STRUCTURE);
		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		StructureUpdateForm beanForm = (StructureUpdateForm) form;
		beanForm.setIsAddNew(false);
		if(request.getParameter(StringConstants.BTN_EDIT) == null){
			int id = Integer.parseInt(request.getParameter("id"));
			HreSubunit tmp = baseService.getPojo(id, HreSubunit.class);
			beanForm.setId(id);
			beanForm.setLevel(tmp.getLevel());
			beanForm.setName(CommonUtils.normalizeString(tmp.getName()));
			beanForm.setDescription(CommonUtils.normalizeString(tmp.getDescription()));
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))){
			beanForm.setMessage("Name of unit can not be blank!");
			return mapping.findForward(ACTION_NAME);
		}
		HreSubunit tmp = baseService.getPojo(beanForm.getId(), HreSubunit.class);
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setDescription(CommonUtils.normalizeString(beanForm.getDescription()));
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);

		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			return mapping.findForward(ACTION_NAME);
		}
		passiveDynaCache.removeCachedItem(RequestUtils.Attribute.LIST_SUB_UNITS_STRUCTURE);
		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
}
